#include "imagesaveworkerthread.h"
#include <QDebug>
#include <QSettings>
#include <QCoreApplication>
#include <QDir>

using namespace HalconCpp;

ImageSaveWorkerThread::ImageSaveWorkerThread(QObject *parent) :
    QThread(parent)
{
    // qRegisterMetaType<HalconCpp::HRegion> ("HalconCpp::HRegion");
    //qRegisterMetaType<QMap<QString, ResultManager::DATE_RESULT_DEF>>("QMap<QString, ResultManager::DATE_RESULT_DEF>");

}

void ImageSaveWorkerThread::SetLotAndDate(QString lot, QString date)
{
    m_LotName = lot;
    m_Date = date;
}

void ImageSaveWorkerThread::AddImageList(QList<HalconCpp::HImage> imagePadList, QList<HalconCpp::HImage> imageCapList, QList<HalconCpp::HImage> imageSubList, QString lotName, QDateTime time, int result, HImage imageResult, int chipType)
{
    IMAGE_SAVE_THREAD_CTX ctx;
    ctx.time = time;
    ctx.lotId = lotName;
    ctx.imagePadList = imagePadList;
    ctx.imageCapList = imageCapList;
    ctx.imageSubList = imageSubList;
    ctx.result = result;
    ctx.imageResult = imageResult.CopyImage();
	ctx.chipType = chipType;
    m_listCtx.append(ctx);
}

void ImageSaveWorkerThread::run()
{
    qDebug() << __FUNCTION__ << "++";
    while (!m_listCtx.isEmpty())
    {
		qDebug() << "m_listCtx:" << m_listCtx.count();
		//if (!m_listCtx.isEmpty())
		{
			IMAGE_SAVE_THREAD_CTX ctx = m_listCtx.first();
			DoSaveImage(ctx);
			m_listCtx.removeFirst();
		}
    }
	qDebug() << __FUNCTION__ << "--";
}

void ImageSaveWorkerThread::DoSaveImage(IMAGE_SAVE_THREAD_CTX ctx)
{
    qDebug() << __FUNCTION__ << "++";
    QDateTime time1 = QDateTime::currentDateTime();

    QString currentdate = ctx.time.toString("yyyy-MM-dd");
    QString currentime = ctx.time.toString("hh_mm_ss");

    QString dirStr = QString("%1/TempImages/%2").arg(QCoreApplication::applicationDirPath()).arg(currentdate);
    if (ctx.lotId.length() > 0)
    {
        dirStr = QString("%1/%2").arg(dirStr).arg(ctx.lotId);
    }
//	if (m_inspectParam.bSeperateFolder)
//	{
//		dirStr = QString("%1/%2/%3_%4").arg(m_path).arg(currentdate).arg(LgaPackageManager::getInstance()->getPackageType()).arg(m_iInspectCount / m_inspectParam.iCountForSeperateFolder + 1);
//		m_iInspectCount++;
//	}
    QDir dir;
    if (!dir.exists(dirStr))
    {
        dir.mkpath(dirStr);
        //qDebug() << "mkpath" << dir_str << res;
    }


    {
        QString appName = QCoreApplication::applicationName();
        QSettings app(appName, "Debug");
        QStringList list;
        list << "bmp" << "png fastest" << "png";

        int index = app.value("imagetype").toInt();
        if (index >= list.count() || index < 0)
        {
            index = 0;
        }
        QString ext = list[index];
        for (int i = 0; i < ctx.imagePadList.count(); i++)
        {
            HImage image = ctx.imagePadList[i];

            if (image.IsInitialized())
            {
                QString file = QString("%1/%2_pad%3").arg(dirStr).arg(currentime).arg(i+1);
				if (ctx.chipType == 0)
				{
					file = QString("%1/%2_ball%3").arg(dirStr).arg(currentime).arg(i + 1);
				}
                image.WriteImage(ext.toUtf8(), 0, file.toUtf8());
            }
        }
        for (int i = 0; i < ctx.imageSubList.count(); i++)
        {
            HImage image = ctx.imageSubList[i];

            if (image.IsInitialized())
            {
                QString file = QString("%1/%2_edge%3").arg(dirStr).arg(currentime).arg(i+1);
                image.WriteImage(ext.toUtf8(), 0, file.toUtf8());
            }
        }
        for (int i = 0; i < ctx.imageCapList.count(); i++)
        {
            HImage image = ctx.imageCapList[i];

            if (image.IsInitialized())
            {
                QString file = QString("%1/%2_cap%3").arg(dirStr).arg(currentime).arg(i+1);
                image.WriteImage(ext.toUtf8(), 0, file.toUtf8());
            }
        }
        QString str = ctx.result ? "P" : "F";
        QString fileReuslt = QString("%1/%2_%3").arg(dirStr).arg(currentime).arg(str);
        ctx.imageResult.WriteImage(ext.toUtf8(), 0, fileReuslt.toUtf8());
//		QHalconWindow *imageView = (QHalconWindow *)m_widget;
//		imageView->GetHalconBuffer()->DumpWindow(m_imgType.toUtf8(), fileReuslt.toUtf8());
    }
    QDateTime time2 = QDateTime::currentDateTime();
    qint64 msec = time1.msecsTo(time2);
    qDebug() << __FUNCTION__ << "--" << msec;
}
